package com.apes.fn.fico.reimbursement.model;

import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.scm.masterdata.finance.model.BankAccount;
import com.apes.scm.masterdata.finance.model.PaymentMethodType;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;

/**
 * Created by lishikun on 2021/4/22 13:49
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicUpdate
@DynamicInsert
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = TransactionFlowItem.CONST_TABLE_NAME)
public class TransactionFlowItem  extends BaseModel {

    public static final String CONST_TABLE_NAME = "fn_transaction_flow_item";

    @Id
    @Column(name = "id", length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "交易流水明细", padding = 7)
    @Comment("交易流水明细编码")
    private String id;

//    @NotNull
    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="transaction_flow_id", referencedColumnName="id")
    @Comment("交易流水")
    private TransactionFlow transactionFlow;

    @Column(name = "amount", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("金额")
    private double amount = 0;

    @Column(name = "used_amount",  columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("已使用交易额")
    private double usedAmount = 0;

    @Column(name = "pay_amount",  columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("付款金额")
    private double payAmount = 0;


    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "reimbursement_id", referencedColumnName = "id")
    @Comment("费用报销")
    private Reimbursement reimbursement;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "expense_debit_id", referencedColumnName = "id")
    @Comment("费用借支")
    private ExpenseDebit expenseDebit;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "debit_repayment_id", referencedColumnName = "id")
    @Comment("借支还款")
    private DebitRepayment debitRepayment;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "bankAccount_id", referencedColumnName = "id")
    @Comment("银行账户")
    private BankAccount bankAccount;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="payment_method_type", referencedColumnName="id")
    @Comment("付款方式")
    private PaymentMethodType paymentMethodType;

    @Column(name = "note", length = 280)
    @Comment("备注")
    private String note;

    @Transient
    @Comment("银行帐号")
    private String bankAccountId;

    public String getBankAccountId(){
        String code = null;
        if (bankAccount != null) {
            code = bankAccount.getAccountId();
        }
        return code;
    }

    @Transient
    private double paymentAmount;

    @Transient
    private String bankId;

    @Transient
    private String bankName;


    @Transient
    private String accountId;

}
